package com.czh.springboot04.MyController;


import com.czh.springboot04.entity.Course;
import com.czh.springboot04.service.CourseService;
import com.czh.springboot04.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseColntorller {
    @Autowired
    private CourseService courseService;

    @GetMapping("/findAll")
    public R findAllCourse(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,@RequestParam(value = "pageSize",required = false,defaultValue = "2") int pageSize){
        PageInfo<Course> course = courseService.findAll(pageNum, pageSize);
        return R.ok(course);
    }
    @PutMapping ("addcourse")
    public R addCourse(@RequestBody Course course){
        int i = courseService.addCourse(course);
        if(i>0) {
            return R.ok("添加成功");
        }else {
            return R.fail("添加失败");
        }
    }

    @PutMapping ("updatecourse")
    public R updateCourse(@RequestBody Course course){
        int i = courseService.updateCourse(course);
        if(i>0) {
            return R.ok("修改成功");
        }else {
            return R.fail("修改失败");
        }
    }
    @PutMapping ("deletecourse")
    public R deleteCourse(int id){
        int i = courseService.deleteCourse(id);
        if(i>0) {
            return R.ok("删除成功");
        }else {
            return R.fail("删除失败");
        }
    }
    //根据咨询师id查询
    @PutMapping ("findcoursebyid")
    public R findCourseById(int id,@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,@RequestParam(value = "pageSize",required = false,defaultValue = "2") int pageSize){
        System.out.println(id);
        PageInfo<Course> course = courseService.findCourseById(id,pageNum, pageSize);
        if(course!=null) {
            return R.ok(course);
        }else {
            return R.fail("查询失败");
        }
    }
    //根据课程名称模糊查询
    @GetMapping ("/selectcoursebyname")
    public R selectCourseByName(String courseName,int pageNum,int pageSize){
        PageInfo<Course> coursePageInfo = courseService.selectCourseByName(courseName,pageNum, pageSize);
        if(coursePageInfo!=null) {
            return R.ok(coursePageInfo);
        }else {
            return R.fail("查询失败");
        }
    }
    //根据价格升序查询
    @GetMapping ("/selectcoursebypriceasc")
    public R selectCourseByPriceAsc(int pageNum,int pageSize){
        PageInfo<Course> coursePageInfo = courseService.selectCourseByPriceAsc(pageNum, pageSize);
        if(coursePageInfo!=null) {
            return R.ok(coursePageInfo);
        }else {
            return R.fail("查询失败");
        }
    }
    //根据价格降序查询
    @GetMapping ("/selectcoursebypricedesc")
    public R selectCourseByPriceDesc(int pageNum,int pageSize){
        PageInfo<Course> coursePageInfo = courseService.selectCourseByPriceDesc(pageNum, pageSize);
        if(coursePageInfo!=null) {
            return R.ok(coursePageInfo);
        }else {
            return R.fail("查询失败");
        }
    }
    //根据价格区间查询
    @GetMapping ("/selectcoursebyprice")
    public R selectCourseByPrice(double min,double max,int pageNum,int pageSize){
        PageInfo<Course> coursePageInfo = courseService.selectCourseByPrice(min, max,pageNum, pageSize);
        if(coursePageInfo!=null) {
            return R.ok(coursePageInfo);
        }else {
            return R.fail("查询失败");
        }
    }
    //根据收藏量降序查询
    @GetMapping ("/selectcoursebycollection")
    public R selectCourseByCollection(int pageNum,int pageSize){
        PageInfo<Course> coursePageInfo = courseService.selectCourseByCollection(pageNum, pageSize);
        if(coursePageInfo!=null) {
            return R.ok(coursePageInfo);
        }else {
            return R.fail("查询失败");
        }
    }
    //根据课程类型查询
    @GetMapping ("/selectcoursebytype")
    public R selectCourseByType(int pageNum,int pageSize,int typeId){
        PageInfo<Course> coursePageInfo = courseService.selectCourseByType(pageNum, pageSize,typeId);
        if(coursePageInfo!=null) {
            return R.ok(coursePageInfo);
        }else {
            return R.fail("查询失败");
        }
    }
    //根据课程类型并根据收藏量查询降序
    @GetMapping ("/selectcoursebytypeandcollection")
    public R selectCourseByTypeAndCollection(int pageNum,int pageSize,int typeId){
        PageInfo<Course> coursePageInfo = courseService.selectCourseByTypeAndCollection(pageNum, pageSize,typeId);
        if(coursePageInfo!=null) {
            return R.ok(coursePageInfo);
        }else {
            return R.fail("查询失败");
        }
    }
    //根据课程类型并根据价格查询升序查询
    @GetMapping ("/selectcoursebytypeandpriceasc")
    public R selectCourseByTypeAndPriceAsc(int pageNum,int pageSize,int typeId){
        PageInfo<Course> coursePageInfo = courseService.selectCourseByTypeAndPriceAsc(pageNum, pageSize,typeId);
        if(coursePageInfo!=null) {
            return R.ok(coursePageInfo);
        }else {
            return R.fail("查询失败");
        }
    }
    //根据课程类型并根据价格查询降序查询
    @GetMapping ("/selectcoursebytypeandpricedesc")
    public R selectCourseByTypeAndPriceDesc(int pageNum,int pageSize,int typeId){
        PageInfo<Course> coursePageInfo = courseService.selectCourseByTypeAndPriceDesc(pageNum, pageSize,typeId);
        if(coursePageInfo!=null) {
            return R.ok(coursePageInfo);
        }else {
            return R.fail("查询失败");
        }
    }
    //根据课程类型并根据价格区间查询
    @GetMapping ("/selectcoursebytypeandprice")
    public R selectCourseByTypeAndPrice(int pageNum,int pageSize,double min,double max,int typeId){
        PageInfo<Course> coursePageInfo = courseService.selectCourseByTypeAndPrice(pageNum, pageSize,min, max,typeId);
        if(coursePageInfo!=null) {
            return R.ok(coursePageInfo);
        }else {
            return R.fail("查询失败");
        }
    }
    //根据课程id查询
    @PutMapping ("findcoursebycourseid")
    public R findCourseByCourseId(int id){
        Course course = courseService.findCourseByCourseId(id);
        if(course!=null) {
            return R.ok(course);
        }else {
            return R.fail("查询失败");
        }
    }
}
